"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.errLogger = exports.ctxLogger = void 0;
const path_1 = __importDefault(require("path"));
const koa_log4_1 = __importDefault(require("koa-log4"));
koa_log4_1.default.configure({
    appenders: {
        request: {
            type: "dateFile",
            pattern: "-yyyy-MM-dd.log",
            filename: path_1.default.join(`./logs`, "request.log") //生成文件名,记录请求信息
        },
        error: {
            type: "dateFile",
            pattern: "-yyyy-MM-dd.log",
            filename: path_1.default.join(`./logs`, "error.log") //生成文件名,记录系统错误信息
        },
        response: {
            type: "dateFile",
            pattern: "-yyyy-MM-dd.log",
            filename: path_1.default.join(`./logs`, "response.log")
        },
        out: {
            type: "console"
        }
    },
    categories: {
        default: { appenders: ["out"], level: "info" },
        request: { appenders: ["request"], level: "info" },
        response: { appenders: ["response"], level: "info" },
        error: { appenders: ["error"], level: "WARN" }
    }
});
const ctxLogger = async function (ctx, next) {
    let logText = `ip:${ctx.ip},method:${ctx.method},url:${ctx.url},origin:${ctx.origin}`;
    if (ctx.state.user) {
        logText += `,user:${ctx.state.user.code},userName:${ctx.state.user.name}`;
    }
    koa_log4_1.default.getLogger("request").info(logText);
    await next();
    koa_log4_1.default.getLogger("response").info(ctx.body);
};
exports.ctxLogger = ctxLogger;
const errLogger = koa_log4_1.default.getLogger("error"); //记录所有错误日志
exports.errLogger = errLogger;
